package com.supermarket_project.service.impl;

import com.supermarket_project.common.R;
import com.supermarket_project.mapper.StaffMapper;
import com.supermarket_project.pojo.Staff;
import com.supermarket_project.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ljc
 * Date: 2022/07/07 9:19
 * Description:
 * Version: V1.0
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public R staffSelect(Integer page, Integer limit, Integer staff_id) {
        System.out.println("=====用户需要查看信息=====");
        R r = new R<>();
        Integer start = 0;
        Integer off = 10;
        List<Staff> staffList = staffMapper.staffSelect(start, off, staff_id);
        try {
            if (staffList != null) {
                r.setCode(200);
                r.setMsg("获取到了数据库的数据");
                r.setData(staffList);
                return r;
            } else {
                r.setCode(200);
                r.setMsg("数据库数据为空");
                r.setData(null);
                return r;
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("数据库连接失败");
            r.setData(null);
            return r;
        }
    }

    @Override
    public R loginstaff(Integer staff_id, String password) {
        System.out.println("=====用户需要登录=====");
        R r = new R<>();
        Staff staff = null;
        try {
            staff = staffMapper.loginStaff(staff_id, password);
            if (staff_id != null && staff.getPassword().equals(password)) {
                r.setCode(200);
                r.setMsg("登录成功");
                r.setData(staff);
                return r;
            } else {
                r.setCode(500);
                r.setMsg("账号密码有错");
                return r;
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("账号密码有错");
            r.setData(null);
            return r;
        }
    }

    @Override
    public R addStaff(Staff staff) {
        System.out.println("=====用户需要添加员工信息=====");
        R r = new R<>();
        int result = 0;
        try {
            result = staffMapper.addStaff(staff);
            if (result > 0) {
                r.setCode(200);
                r.setMsg("插入成功");
                return r;
            } else {
                r.setCode(500);
                r.setMsg("插入失败");
                return r;
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("数据库连接失败");
            return r;
        }
    }

    @Override
    public R delStaff(Integer staff_id) {
        System.out.println("======管理员需要删除信息======");
        R r=new R<>();
        int result=0;
        try {
            result= staffMapper.delStaff(staff_id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
                return r;
            }else {
                r.setCode(500);
                r.setMsg("删除失败，请检查");
                return r;
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("数据库连接失败");
            return r;
        }
    }

    @Override
    public R upStaff(Staff staff) {
        System.out.println("======管理员修改员工信息======");
        R r=new R<>();
        int result=0;
        try {
            result=staffMapper.upStaff(staff.getStaff_id(),staff.getSix(),staff.getHierarchy(),staff.getStatus());
            if (result>0){
                r.setCode(200);
                r.setMsg("修改失败");
                return r;
            }else {
                r.setCode(500);
                r.setMsg("修改失败失败，请检查");
                return r;
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("数据库连接失败");
            return r;
        }
    }

    @Override
    public R regStaff(Integer staff_id) {
        System.out.println("======管理员需要恢复员工状态======");
        R r=new R<>();
        int result=0;
        try {
            result= staffMapper.regStaff(staff_id);
            if (result>0){
                r.setCode(200);
                r.setMsg("恢复成功");
                return r;
            }else {
                r.setCode(500);
                r.setMsg("恢复失败，请检查");
                return r;
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("数据库连接失败");
            return r;
        }
    }
}
